package com.idle.view;

import com.idle.android.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.FontMetrics;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;


/**
 * @author zcw
 * @version 1.0
 */
public class ProgressView extends View {
	
	private Paint mCirclePaint; // 画实心圆的画笔
	private Paint mRingPaint; // 画圆环的画笔
	private Paint mTextPaint; // 画字体的画笔
	private int mCircleColor; // 圆形颜色
	
	private int mRingColor; // 圆环颜色
	private float mRadius; // 半径
	private float mRingRadius; // 圆环半径
	private float mStrokeWidth; // 圆环宽度
	
	private int mXCenter; // 圆心x坐标
	private int mYCenter; // 圆心y坐标
	private float mTxtWidth; // 字的长度
	private float mTxtHeight; // 字的高度
	
	private int mTotalProgress = 100; // 总进度
	private int mProgress; // 当前进度
	private float mTotalTime = 0f;
	private boolean isShowPercentage = false;

	public ProgressView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// 获取自定义的属性
		initAttrs(context, attrs);
		initVariable();
	}

	private void initAttrs(Context context, AttributeSet attrs) {
		TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
				R.styleable.ProgressView, 0, 0);
		mRadius = typeArray.getDimension(R.styleable.ProgressView_radius, 1);
		mStrokeWidth = typeArray.getDimension(R.styleable.ProgressView_strokeWidth, 10);
		mCircleColor = typeArray.getColor(R.styleable.ProgressView_circleColor, 0xFFFFFFFF);
		mRingColor = typeArray.getColor(R.styleable.ProgressView_ringColor, 0xFFFFFFFF);
		
		mRingRadius = mRadius + mStrokeWidth / 2;
	}

	private void initVariable() {
		mCirclePaint = new Paint();
		mCirclePaint.setAntiAlias(true);
		mCirclePaint.setColor(mCircleColor);
		mCirclePaint.setStyle(Paint.Style.FILL);
		
		mRingPaint = new Paint();
		mRingPaint.setAntiAlias(true);
		mRingPaint.setColor(mRingColor);
		mRingPaint.setStyle(Paint.Style.STROKE);
		mRingPaint.setStrokeWidth(mStrokeWidth);
		
		mTextPaint = new Paint();
		mTextPaint.setAntiAlias(true);
		mTextPaint.setStyle(Paint.Style.FILL);
		mTextPaint.setARGB(255, 255, 255, 255);
		mTextPaint.setTextSize(mRadius / 2);
		
		FontMetrics fm = mTextPaint.getFontMetrics();
		mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent);
		
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		/*mRadius = getWidth()/ 2-getPaddingLeft();//TODO
		mRingRadius = mRadius - mStrokeWidth;*/
		
		mXCenter = getWidth() / 2;
		mYCenter = getHeight() / 2;
		
		canvas.drawCircle(mXCenter, mYCenter, mRadius, mCirclePaint);
		
		if (mProgress > 0 ) {
			RectF oval = new RectF();
			oval.left = (mXCenter - mRingRadius);
			oval.top = (mYCenter - mRingRadius);
			oval.right = (mXCenter + mRingRadius);
			oval.bottom = (mYCenter + mRingRadius);
			canvas.drawArc(oval, -90, ((float)mProgress / mTotalProgress) * 360, false, mRingPaint); //
			if (isShowPercentage) {
				String txt = mProgress + "%";
				mTxtWidth = mTextPaint.measureText(txt, 0, txt.length());//不显示数字进度
				canvas.drawText(txt, mXCenter - mTxtWidth / 2, mYCenter + mTxtHeight / 4, mTextPaint);
			}
		}
	}
	/**
	 * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 */
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
	
	private Handler mHandler = new Handler();
	Runnable timingRunnable = new Runnable() {
		
		@Override
		public void run() {
			/*while (timing && mProgress < mTotalProgress) {
				mProgress++;
				setProgress(mProgress);
				postInvalidate();
				try {
					Thread.sleep((long)mTotalTime/mTotalProgress);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			timing = false;
			setProgress(0);
			postInvalidate();*/
			if (mProgress < mTotalProgress) {
				mProgress++;
		        invalidate();
		        mHandler.postDelayed(timingRunnable, (long)mTotalTime/mTotalProgress);
			}else {
				mProgress = 0;
				mHandler.removeCallbacks(timingRunnable);
			}
			setProgress(mProgress);
			/*if (onProgressChangeListener != null) {
				onProgressChangeListener.onChange(mProgress);
			}*/
		}
	};
	
	public void setProgress(int progress) {
		mProgress = progress;
		postInvalidate();
		post(new Runnable() {
			
			@Override
			public void run() {
				if (onProgressChangeListener != null) {
					onProgressChangeListener.onChange(mProgress);
				}
			}
		});
	}
	
	public void setProgressAndStartTiming(int progress) {
		setProgress(progress);
		mHandler.removeCallbacks(timingRunnable);
		mHandler.postDelayed(timingRunnable, 0);
		/*if (!timing) {
			timing = true;
			new Thread(timingRunnable).start();
		}*/
	}
	
	public void setTotalProgress(int otalProgress) {
		mTotalProgress = otalProgress;
	}

	public void setTotalTime(float mTotalTime) {
		this.mTotalTime = mTotalTime;
	}

	public boolean isShowPercentage() {
		return isShowPercentage;
	}

	public void setShowPercentage(boolean isShowPercentage) {
		this.isShowPercentage = isShowPercentage;
		invalidate();
	}

	//boolean timing = false;
	public void startTiming() {
		mProgress = 0;
		mHandler.removeCallbacks(timingRunnable);
		mHandler.postDelayed(timingRunnable, 0);
		/*if (!timing) {
			timing = true;
			new Thread(timingRunnable).start();
		}*/
	}
	
	public void stopTiming() {
		//timing = false;
		mHandler.removeCallbacks(timingRunnable);
	}
	
	OnProgressChangeListener onProgressChangeListener;
	public interface OnProgressChangeListener{
		public void onChange(int progress);
	}
	public void setOnProgressChangeListener(
			OnProgressChangeListener onProgressChangeListener) {
		this.onProgressChangeListener = onProgressChangeListener;
	}
	
}
